//
//  RegisterViewController.m
//  Cycling
//
//  Created by cjg.mini on 2023/3/3.
//

#import "RegisterViewController.h"
#import "UserRequest.h"
#import "PerfectMessageViewController.h"
#import "BaseNavigationController.h"
#import "BaseTabBarController.h"
#import "AppDelegate.h"
#import "GoogleSignInHelper.h"
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import "CycLabel.h"
#import "ProtocolViewController.h"

#import "FacebookSignInHelper.h"

@interface RegisterViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *accountF;
@property (nonatomic, strong) UITextField *codeF;
@property (nonatomic, strong) UIButton *codeBtn;
@property (nonatomic, strong) UITextField *pwdF;
@property (nonatomic, strong) UIButton *eyeBtn;
@property (nonatomic, strong) UITextField *surepwdF;
@property (nonatomic, strong) UIButton *sureEyeBtn;
@property (nonatomic, strong) UIButton *registerBtn;
@property (nonatomic) dispatch_source_t timerGCD;

@property (nonatomic, strong) UIButton *protocolBtn;
@property (nonatomic, strong) CycLabel *protocolLB;
@property (nonatomic, assign) BOOL isSelect;

@property (nonatomic, strong) UIView *otherLeftSignView;
@property (nonatomic, strong) UILabel *otherAccountLbl;
@property (nonatomic, strong) UIView *otherRightSignView;
@property (nonatomic, strong) UIButton *googleButton;
@property (nonatomic, strong) UIButton *facebookButton;

@end

@implementation RegisterViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];

}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isSelect = NO;

    [self configUI];
    
    if (self.type == ControllerTypeForget) {
        self.title = CycLocalizedString(@"forgot password", @"");
    } else {
        self.title = CycLocalizedString(@"register", @"");
        UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [loginBtn setTitle:CycLocalizedString(@"Login", @"") forState:UIControlStateNormal];
        [loginBtn setTitleColor:rgb(128, 128, 128) forState:UIControlStateNormal];
        loginBtn.titleLabel.font = [UIFont systemFontOfSize:22];
        [loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];

        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:loginBtn];
        
        [self requestReviewAppVersion];
        [BaseRequest checkoutNetWorkStatus:^(AFNetworkReachabilityStatus status) {
            if(status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
                [self requestReviewAppVersion];
            }
        }];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)configUI {
    UIScrollView *bgView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    bgView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view).offset(0);
    }];

    UIImageView *topImageView = [[UIImageView alloc] init];
    topImageView.image = [UIImage imageNamed:@"路径"];
    [bgView addSubview:topImageView];
    [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView.mas_top).offset(18);
        make.leading.equalTo(bgView.mas_leading).offset(24);
        make.trailing.equalTo(bgView.mas_trailing).offset(-24);
        make.size.mas_equalTo(CGSizeMake(SCREEN_W-48, (SCREEN_W-48)*132/329));
    }];
    
    UILabel *titleL = [[UILabel alloc] init];
    titleL.text = CycLocalizedString(@"Welcome to emojo", @"欢迎来到骑行");
    titleL.textColor = rgb(48, 48, 48);
    titleL.numberOfLines = 0;
    titleL.font = [UIFont systemFontOfSize:28];
    [bgView addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topImageView.mas_left).offset(3);
        make.top.equalTo(topImageView.mas_top).offset(12);
    }];
    UIView *titleView = [[UIView alloc] init];
    titleView.backgroundColor = rgb(10, 211, 159);
    [bgView addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleL.mas_left);
        make.top.equalTo(titleL.mas_bottom).offset(5);
        make.width.offset(38);
        make.height.offset(4);
    }];
    
    UITextField *accountF = [[UITextField alloc] init];
    accountF.placeholder = CycLocalizedString(@"Mail", "邮箱");
    accountF.delegate = self;
    accountF.keyboardType = UIKeyboardTypeDefault;
    accountF.textColor = rgbHex(0xA6A6A6,1.0);
    accountF.clearButtonMode = UITextFieldViewModeWhileEditing;
    accountF.borderStyle = UITextBorderStyleNone;
    [bgView addSubview:accountF];
    self.accountF = accountF;
    [accountF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(28);
        make.top.equalTo(topImageView.mas_bottom).offset(5);
        make.right.equalTo(bgView.mas_right).offset(-28);
        make.height.offset(50);
    }];
    
    UIView *accountView = [[UIView alloc] init];
    accountView.backgroundColor = rgb(238, 238, 238);
    [bgView addSubview:accountView];
    [accountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(31);
        make.top.equalTo(accountF.mas_bottom).offset(0);
        make.right.equalTo(bgView.mas_right).offset(-31);
        make.height.offset(1);
    }];
    
    UITextField *codeF = [[UITextField alloc] init];
    codeF.placeholder = CycLocalizedString(@"Security code", @"验证码");
    codeF.delegate = self;
    codeF.borderStyle = UITextBorderStyleNone;
    codeF.textColor = rgbHex(0xA6A6A6,1.0);
    codeF.clearButtonMode = UITextFieldViewModeWhileEditing;
    codeF.keyboardType = UIKeyboardTypeDefault;
    [bgView addSubview:codeF];
    self.codeF = codeF;
    [codeF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(28);
        make.top.equalTo(accountF.mas_bottom).offset(17);
        make.right.equalTo(bgView.mas_right).offset(-184);
        make.height.offset(50);
    }];
    
    UIView *codeView = [[UIView alloc] init];
    codeView.backgroundColor = rgb(238, 238, 238);
    [bgView addSubview:codeView];
    [codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(28);
        make.top.equalTo(codeF.mas_bottom).offset(0);
        make.right.equalTo(bgView.mas_right).offset(-28);
        make.height.offset(1);
    }];
    
    UIButton *codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [codeBtn setTitle:CycLocalizedString(@"Verification code", @"验证码") forState:UIControlStateNormal];
    [codeBtn setTitleColor:rgb(10, 211, 159) forState:UIControlStateNormal];
    [codeBtn addTarget:self action:@selector(sendCode:) forControlEvents:UIControlEventTouchUpInside];
    codeBtn.layer.borderWidth = 0.6;
    codeBtn.layer.borderColor = COLOR_TOPIC_SELECT.CGColor;
    codeBtn.layer.cornerRadius = 4;
    codeBtn.layer.masksToBounds = YES;
    [bgView addSubview:codeBtn];
    self.codeBtn = codeBtn;
    [codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView.mas_right).offset(-32);
        make.centerY.equalTo(codeF.mas_centerY);
        make.width.offset(120);
        make.height.offset(38);
    }];
    
    UITextField *pwdF = [[UITextField alloc] init];
    pwdF.placeholder = CycLocalizedString(@"Password", @"密码");
    pwdF.delegate = self;

    pwdF.borderStyle = UITextBorderStyleNone;
    pwdF.textColor = rgbHex(0xA6A6A6,1.0);
    pwdF.clearButtonMode = UITextFieldViewModeWhileEditing;
    pwdF.keyboardType = UIKeyboardTypeDefault;
    [bgView addSubview:pwdF];
    self.pwdF = pwdF;
    [pwdF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(28);
        make.top.equalTo(codeView.mas_bottom).offset(17);
        make.right.equalTo(bgView.mas_right).offset(-104);
        make.height.offset(50);
    }];
    
    UIView *pwdView = [[UIView alloc] init];
    pwdView.backgroundColor = rgb(238, 238, 238);
    [bgView addSubview:pwdView];
    [pwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(28);
        make.top.equalTo(pwdF.mas_bottom).offset(0);
        make.right.equalTo(bgView.mas_right).offset(-28);
        make.height.offset(1);
    }];
    
    UIButton *eyeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.pwdF.secureTextEntry = YES;
    [eyeBtn setImage:[UIImage imageNamed:@"pwd_close"] forState:UIControlStateNormal];
    
    [eyeBtn addTarget:self action:@selector(eyeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:eyeBtn];
    self.eyeBtn = eyeBtn;
    [eyeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView.mas_right).offset(-26);
        make.centerY.equalTo(pwdF.mas_centerY);
        make.width.offset(46);
        make.height.offset(46);
    }];
    
    UITextField *surepwdF = [[UITextField alloc] init];
    surepwdF.placeholder = CycLocalizedString(@"Confirm password", @"确认密码");
    surepwdF.delegate = self;
    surepwdF.borderStyle = UITextBorderStyleNone;
    surepwdF.textColor = rgbHex(0xA6A6A6,1.0);
    surepwdF.clearButtonMode = UITextFieldViewModeWhileEditing;
    surepwdF.keyboardType = UIKeyboardTypeDefault;
    [bgView addSubview:surepwdF];
    self.surepwdF = surepwdF;
    [surepwdF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(31);
        make.top.equalTo(pwdF.mas_bottom).offset(17);
        make.right.equalTo(bgView.mas_right).offset(-104);
        make.height.offset(50);
    }];
    
    UIView *surepwdView = [[UIView alloc] init];
    surepwdView.backgroundColor = rgb(238, 238, 238);
    [bgView addSubview:surepwdView];
    [surepwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(28);
        make.top.equalTo(surepwdF.mas_bottom).offset(0);
        make.right.equalTo(bgView.mas_right).offset(-28);
        make.height.offset(1);
    }];
    
    UIButton *sureEyeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.surepwdF.secureTextEntry = YES;
    [sureEyeBtn setImage:[UIImage imageNamed:@"pwd_close"] forState:UIControlStateNormal];
    
    [sureEyeBtn addTarget:self action:@selector(sureEyeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:sureEyeBtn];
    self.sureEyeBtn = sureEyeBtn;
    [sureEyeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView.mas_right).offset(-26);
        make.centerY.equalTo(surepwdF.mas_centerY);
        make.width.offset(46);
        make.height.offset(46);
    }];
    
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    if (self.type == ControllerTypeRegister) {
        [registerBtn setTitle:CycLocalizedString(@"register", @"") forState:UIControlStateNormal];
    } else {
        [registerBtn setTitle:CycLocalizedString(@"Finish", @"") forState:UIControlStateNormal];
    }
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registerBtn setBackgroundImage:[UIImage imageNamed:@"btn_shense"] forState:UIControlStateNormal];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:24];
    [registerBtn addTarget:self action:@selector(registerAccount) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:registerBtn];
    self.registerBtn = registerBtn;
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(26);
        make.right.equalTo(bgView.mas_right).offset(-26);
        make.top.equalTo(surepwdF.mas_bottom).offset(20);
        make.height.offset(62);
    }];
    
    self.protocolBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.protocolBtn setBackgroundImage:[UIImage imageNamed:@"icon_not_select"] forState:UIControlStateNormal];
    [self.protocolBtn addTarget:self action:@selector(protocolBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:self.protocolBtn];
    [self.protocolBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(26);
        make.top.equalTo(registerBtn.mas_bottom).offset(5);
        make.size.mas_equalTo(CGSizeMake(25, 25));
    }];
    self.protocolLB = [[CycLabel alloc] init];
    self.protocolLB.font = FONT_SECONDARY_TITLE;
    self.protocolLB.textColor = COLOR_BLACK;
    [self.protocolLB setText:CycLocalizedString(@"Agree our Privacy policy and Term of service", @"同意隐私政策和用户协议") HighlightUserText:CycLocalizedString(@"Term of service", @"用户协议") action:^{
        ProtocolViewController *vc = [[ProtocolViewController alloc] init];
        vc.htmlStr = Cyc_WEBURL_USER;
        vc.title = CycLocalizedString(@"Term of service", @"用户协议");
        [self.navigationController pushViewController:vc animated:YES];
    } HighlightPrivacyText:CycLocalizedString(@"Privacy policy", @"隐私政策") action:^{
        ProtocolViewController *vc = [[ProtocolViewController alloc] init];
        vc.htmlStr = Cyc_WEBURL_PRIVACY;
        vc.title = CycLocalizedString(@"Privacy policy", @"隐私政策");
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [bgView addSubview:self.protocolLB];
    [self.protocolLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.protocolBtn.mas_right).offset(10);
        make.right.equalTo(bgView).offset(-26);
        make.centerY.mas_equalTo(self.protocolBtn.mas_centerY);
        make.height.offset(30);
    }];

    UILabel *alertL = [[UILabel alloc] init];
    alertL.text = CycLocalizedString(@"Password must be between 8-16 in length, with both numbers and letters, upper and lower capitals", @"备注:密码必须为字母+数字，长度范围为8～16位，区分大小写。");
    alertL.textColor = rgb(48, 48, 48);
    alertL.numberOfLines = 0;
    alertL.font = [UIFont systemFontOfSize:20];
    [bgView addSubview:alertL];
    [alertL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(26);
        make.right.equalTo(bgView.mas_right).offset(-26);
        make.top.equalTo(self.protocolLB.mas_bottom).offset(10);
    }];
    
    if (self.type == ControllerTypeRegister) {
        UILabel *otherAccountL = [[UILabel alloc] init];
        otherAccountL.text = CycLocalizedString(@"Other accounts", @"其他账户");
        otherAccountL.textColor = rgb(128, 128, 128);
        otherAccountL.textAlignment = NSTextAlignmentCenter;
        otherAccountL.font = [UIFont systemFontOfSize:22];
        [bgView addSubview:otherAccountL];
        [otherAccountL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(bgView);
            make.top.equalTo(alertL.mas_bottom).offset(40);
        }];
        self.otherAccountLbl = otherAccountL;
        
        UIView *leftLineView = [[UIView alloc] init];
        leftLineView.backgroundColor = rgb(238, 238, 238);
        [bgView addSubview:leftLineView];
        [leftLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bgView.mas_left).offset(31);
            make.right.equalTo(otherAccountL.mas_left).offset(-33);
            make.centerY.equalTo(otherAccountL.mas_centerY);
            make.height.offset(1);
        }];
        self.otherLeftSignView = leftLineView;
        
        UIView *rightLineView = [[UIView alloc] init];
        rightLineView.backgroundColor = rgb(238, 238, 238);
        [bgView addSubview:rightLineView];
        [rightLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(otherAccountL.mas_right).offset(33);
            make.centerY.equalTo(otherAccountL.mas_centerY);
            make.right.equalTo(bgView.mas_right).offset(-31);
            make.height.offset(1);
        }];
        self.otherRightSignView = rightLineView;
        
        UIButton *facebookBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [facebookBtn setImage:[UIImage imageNamed:@"Facebook"] forState:UIControlStateNormal];
        self.facebookButton = facebookBtn;
        
        [facebookBtn addTarget:self action:@selector(facebookLogin) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:facebookBtn];
        [facebookBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(leftLineView.mas_bottom).offset(32);
            make.bottom.equalTo(bgView).offset(-SafeAreaBottomHeight-20);
            make.centerX.equalTo(bgView.mas_centerX).offset(-41);
            make.width.offset(44);
            make.height.offset(44);
        }];
        
        UIButton *googleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [googleBtn setImage:[UIImage imageNamed:@"goole"] forState:UIControlStateNormal];
        
        [googleBtn addTarget:self action:@selector(googleLogin) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:googleBtn];
        [googleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(leftLineView.mas_bottom).offset(32);
            make.bottom.equalTo(bgView).offset(-SafeAreaBottomHeight-20);
            make.centerX.equalTo(bgView.mas_centerX).offset(41);
            make.width.offset(44);
            make.height.offset(44);
        }];
        self.googleButton = googleBtn;
        [self showOrHiddenOtherAccountLogin:NO];
    }
    
}

- (void)registerAccount {
    if (IsStrEmpty(self.accountF.text)) {
        [HUDHelper showCustomAlertMessage:CycLocalizedString(@"Email", "请输入邮箱")];
        return;
    }
    if (IsStrEmpty(self.codeF.text)) {
        [HUDHelper showCustomAlertMessage:CycLocalizedString(@"Input verification code", @"请输入验证码")];
        return;
    }
    if (IsStrEmpty(self.pwdF.text)) {
        [HUDHelper showCustomAlertMessage:CycLocalizedString(@"Input Password", "请输入密码")];
        return;
    }
    if (IsStrEmpty(self.surepwdF.text)) {
        [HUDHelper showCustomAlertMessage:CycLocalizedString(@"Input Password", "请输入密码")];
        return;
    }

    if (![self.pwdF.text isEqualToString:self.surepwdF.text]) {
        [HUDHelper showCustomAlertMessage:CycLocalizedString(@"Passwords not match", @"两次密码输入不一致")];
        return;
    }
    if (!self.isSelect) {
        [HUDHelper showCustomAlertMessage:CycLocalizedString(@"Agree our Privacy policy and Term of service", @"请先同意用户隐私协议")];
        return;
    }

    if (self.type == ControllerTypeRegister) {
        [self showLoadingViewWithText:@""];
        [UserRequest registerWithEmail:self.accountF.text code:self.codeF.text password:[NSString stringWithFormat:@"%@",self.pwdF.text] result:^(UserModel *user) {
            [self removeLoadingView];
            
            if (user) {
                [[NSUserDefaults standardUserDefaults] setValue:user.token forKey:@"Token"];
                [user updateUserInfo];
                [UserModel setLoginSatus:YES];
                PerfectMessageViewController *vc = [[PerfectMessageViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
        }];
    } else {
        [self showLoadingViewWithText:@""];
        [UserRequest resetPasswordWithEmail:self.accountF.text captcha:self.codeF.text password:self.pwdF.text result:^(NSInteger code) {
            [self removeLoadingView];
            if (code == 1) {
                [HUDHelper showHudTipStr:CycLocalizedString(@"Passcode change success", @"密码修改成功")];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    }
    
}

- (void)sendCode:(UIButton *)sender {
    if (IsStrEmpty(self.accountF.text)) {
        [HUDHelper showCustomAlertMessage:CycLocalizedString(@"Email", "请输入邮箱")];
        return;
    }
    
    [self showLoadingViewWithText:@""];
    [UserRequest getEmali:self.type == ControllerTypeRegister ? @"register" : @"resetpwd" email:self.accountF.text result:^(BOOL ret) {
        if (ret) {
            [self showNoteCode];
        }
        [self removeLoadingView];
    }];
}
//显示验证码倒计时
- (void)showNoteCode
{
    
    self.codeBtn.userInteractionEnabled = NO;

    __block int timeout = 59;
    _timerGCD = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
    dispatch_source_set_timer(_timerGCD, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timerGCD, ^{
        if(timeout <= 0){
            //倒计时结束，关闭
            dispatch_source_cancel(self->_timerGCD);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.codeBtn setTitle:CycLocalizedString(@"Verification code", @"验证码") forState:UIControlStateNormal];
                self.codeBtn.userInteractionEnabled = YES;
                [self.codeBtn setTitleColor:rgb(10, 211, 159) forState:UIControlStateNormal];
                self.codeBtn.layer.borderColor = COLOR_TOPIC_SELECT.CGColor;
                self.codeBtn.layer.cornerRadius = 4;
                self.codeBtn.layer.masksToBounds = YES;
            });
        }else{
            int seconds = timeout % 60;
            NSString * str = [NSString stringWithFormat:@"%.2ds",seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.codeBtn setTitle:str forState:UIControlStateNormal];
                [self.codeBtn setTitleColor:rgba(10, 211, 159,0.6) forState:UIControlStateNormal];
                self.codeBtn.layer.borderColor = rgba(10, 211, 159,0.6).CGColor;
                self.codeBtn.layer.cornerRadius = 4;
                self.codeBtn.layer.masksToBounds = YES;
            });
        }
        timeout--;
    });
    dispatch_resume(_timerGCD);
}

- (void)facebookLogin
{
    [self openFacebookForLogin];
}

- (void)facebookLoginOld {
    FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
    [loginManager logInWithPermissions:@[@"public_profile"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult * _Nullable result, NSError * _Nullable error) {
         if (error) {
             NSLog(@"Process error");
          } else if (result.isCancelled) {
             NSLog(@"Cancelled");
          } else {
              NSString *facebookId =  result.token.tokenString;
              //用户的facebookId 传给后台 判断该用户是否绑定手机号，如果绑定了直接登录，如果没绑定跳绑定手机号页面
              [UserRequest thirdLoginWithPlatform:@"facebook" code:facebookId result:^(UserModel *user) {
                  if (user) {
                      [[NSUserDefaults standardUserDefaults] setValue:user.token forKey:@"Token"];
                      [user updateUserInfo];
                      [UserModel setLoginSatus:YES];
                      if(user.isNewUser) {
                          PerfectMessageViewController *vc = [[PerfectMessageViewController alloc] init];
                          [self.navigationController pushViewController:vc animated:YES];
                      } else {
                          appDelegate.window.rootViewController = [[BaseNavigationController alloc] initWithRootViewController:[[BaseTabBarController alloc] init]];
                          [appDelegate.window makeKeyAndVisible];
                      }
                  }
              }];
          }
    }];
}

- (void)googleLogin {
    [[GoogleSignInHelper shareInstance] signIn:^(BOOL ret, NSString * _Nonnull token) {
        if(ret){
            [UserRequest thirdLoginWithPlatform:@"google" code:token result:^(UserModel *user) {
                if (user) {
                    [[NSUserDefaults standardUserDefaults] setValue:user.token forKey:@"Token"];
                    [user updateUserInfo];
                    [UserModel setLoginSatus:YES];
                    if(user.isNewUser) {
                        PerfectMessageViewController *vc = [[PerfectMessageViewController alloc] init];
                        [self.navigationController pushViewController:vc animated:YES];
                    } else {
                        appDelegate.window.rootViewController = [[BaseNavigationController alloc] initWithRootViewController:[[BaseTabBarController alloc] init]];
                        [appDelegate.window makeKeyAndVisible];
                    }
                }
            }];
        }else {
            
        }
    } withPresentingVC:self];
}

- (void)eyeBtnClicked:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (!sender.selected) {//隐藏密码
        self.pwdF.secureTextEntry = YES;
        [self.eyeBtn setImage:[UIImage imageNamed:@"pwd_close"] forState:UIControlStateNormal];
    } else {//显示密码
        self.pwdF.secureTextEntry = NO;
        [self.eyeBtn setImage:[UIImage imageNamed:@"pwd_open"] forState:UIControlStateNormal];
    }
}

- (void)sureEyeBtnClicked:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (!sender.selected) {//隐藏密码
        self.surepwdF.secureTextEntry = YES;
        [self.sureEyeBtn setImage:[UIImage imageNamed:@"pwd_close"] forState:UIControlStateNormal];
    } else {//显示密码
        self.surepwdF.secureTextEntry = NO;
        [self.sureEyeBtn setImage:[UIImage imageNamed:@"pwd_open"] forState:UIControlStateNormal];
    }
}

- (void)login {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {

    if (!IsStrEmpty(self.surepwdF.text) && !IsStrEmpty(self.pwdF.text) && !IsStrEmpty(self.codeF.text) && !IsStrEmpty(self.accountF.text)) {
        [self.registerBtn setBackgroundImage:[UIImage imageNamed:@"btn_login"] forState:UIControlStateNormal];
    } else {
        [self.registerBtn setBackgroundImage:[UIImage imageNamed:@"btn_shense"] forState:UIControlStateNormal];
    }
}

// 同意协议
- (void)protocolBtnAction {
    self.isSelect = !self.isSelect;
    if(self.isSelect){
        [self.protocolBtn setBackgroundImage:[UIImage imageNamed:@"icon_select"] forState:UIControlStateNormal];
    }else {
        [self.protocolBtn setBackgroundImage:[UIImage imageNamed:@"icon_not_select"] forState:UIControlStateNormal];
    }

}


- (void)showOrHiddenOtherAccountLogin:(BOOL)willShow
{
    self.otherAccountLbl.hidden = !willShow;
    self.otherLeftSignView.hidden = !willShow;
    self.otherRightSignView.hidden = !willShow;
    self.googleButton.hidden = !willShow;
    self.facebookButton.hidden = !willShow;
}

- (void)requestReviewAppVersion
{
    [UserRequest requestReviewAppVersion:^(NSString * _Nonnull appVersion) {
        if([@"1" isEqualToString:appVersion]){
            [self showOrHiddenOtherAccountLogin:NO];
        } else {
            [self showOrHiddenOtherAccountLogin:YES];
        }
    }];
}

@end
